#include "fromchartview.h"
#include "ui_fromchartview.h"

// #include <QRandomGenerator>

FromChartView::FromChartView(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::FromChartView) {
    ui->setupUi(this);

#ifdef QCHARTS
    createChartView();
#else
    ui->chartview->hide();
    createQCustomPlot();
#endif

    t = new QTimer(this);
    connect(t, &QTimer::timeout, this, &FromChartView::doTimerout);
    // t->start(1000);
}

FromChartView::~FromChartView() {
    delete ui;
}

void FromChartView::setSayCount(int value) {
    sayCount = value;
    // qDebug() << "sayCount:" << sayCount;
}

int FromChartView::getSayCount() {
    return sayCount;
}

void FromChartView::addValue(double value) {
    last_value = value;
}

void FromChartView::setBrowname(QString value) {
#ifdef QCHARTS
    seriesValue->setName(value);
    chart->legend()->setToolTip(value);
#else
    mPlot->xAxis->setLabel(value);
#endif
}

void FromChartView::setAlarmFlag(bool value) {
    // 显示报警灯
    if(value) {
        ui->label_alarm_ico->show();
    } else {
        ui->label_alarm_ico->hide();
    }
    // qDebug() << "Alarm:" << value;
}

void FromChartView::setLimit(double value) {
    last_limit = value;
}

void FromChartView::setShowFlag(bool value) {
    showFlag = value;
    if(value) {
        t->start(1000);
        show();
    } else {
        t->stop();
        hide();
    }
}

bool FromChartView::getShowFlag() {
    return showFlag;
}

void FromChartView::setTheme(int i) {
#ifdef QCHARTS
    chart->setTheme(static_cast<QChart::ChartTheme>(index[i % 7]));
    // chart->setTheme(static_cast<QChart::ChartTheme>(QRandomGenerator::global()->bounded(8)));
    QFont f;
    f.setBold(true);
    f.setPointSize(24);
    f.setFamily("黑体");
    chart->setTitleFont(f);

    QPen p = seriesAlarm->pen();
    // p.setWidth(1);
    p.setColor(Qt::red);
    seriesAlarm->setPen(p);

    if(index[i % 7] == 0) {
        QPen p = seriesValue->pen();
        p.setColor(Qt::blue);
        seriesValue->setPen(p);
    }
#else
    Q_UNUSED(i);
    // QColor PanelColor = QColor(0xea, 0xf7, 0xff);
    // QColor TextColor = QColor(0x38, 0x64, 0x87);
    // QColor BorderColor = QColor(0xc0, 0xdc, 0xf2);
    // QColor NormalColorStart = QColor(0xde, 0xf0, 0xfe);
    // QColor NormalColorEnd = QColor(0xc0, 0xde, 0xf6);
    // QColor DarkColorStart = QColor(0xf2, 0xf9, 0xff);
    // QColor DarkColorEnd = QColor(0xda, 0xef, 0xff);
    // QColor HighColor = QColor(0xff, 0x64, 0x88);
    QLinearGradient plotGradient;
    plotGradient.setStart(0, 0);
    plotGradient.setFinalStop(0, mPlot->height());
    plotGradient.setColorAt(0, NormalColorStart);
    plotGradient.setColorAt(1, NormalColorEnd);
    mPlot->setBackground(plotGradient);

    mPlot->xAxis->setTickLabelColor(TextColor);
    mPlot->yAxis->setTickLabelColor(TextColor);

    mPlot->xAxis->setTickPen(QPen(HighColor));
    mPlot->yAxis->setTickPen(QPen(HighColor));

    mPlot->xAxis->setSubTickPen(QPen(HighColor));
    mPlot->yAxis->setSubTickPen(QPen(HighColor));

    mPlot->xAxis->setBasePen(QPen(HighColor));
    mPlot->yAxis->setBasePen(QPen(HighColor));

    mPlot->xAxis->setLabelColor(TextHighColor);
    title->setTextColor(TextColor);
#endif
}


void FromChartView::setPointSizeX(int i) {
#ifdef QCHARTS
    axisx->setRange(-5, i + 5); //设置x轴范围
#else
    mPlot->xAxis->setRange(-5, i + 5); //设置x轴范围
#endif
    pointSizeX = i;
}

void FromChartView::doTimerout() {
    values.append(last_value);
    limits.append(last_limit);
    if(values.count() > pointSizeX)
        values.remove(0, values.count() - pointSizeX);
    if(limits.count() > pointSizeX)
        limits.remove(0, limits.count() - pointSizeX);
    if(x.count() < pointSizeX)
        x.append(x.count());

    if(!showFlag)
        return;

#ifdef QCHARTS
    seriesValue->clear();
    seriesAlarm->clear();
#endif

    v_max = -999999.0, v_min = 999999.0;

    for(int i = 0; i < values.count(); i++) {
        //! 求最大、最小值
        v_max = v_max < values[i] ? values[i] : v_max;
        v_max = v_max < limits[i] ? limits[i] : v_max;
        v_min = v_min > values[i] ? values[i] : v_min;
        v_min = v_min > limits[i] ? limits[i] : v_min;
#ifdef QCHARTS
        seriesValue->append(i, values[i]);
        seriesAlarm->append(i, limits[i]);
#else
        x[i] = i;
#endif
    }

#ifdef QCHARTS
    double vv = v_max < 10 && v_min < 10 ? 0.2 : 1;
    axisy->setRange(v_min - vv, v_max + vv ); //设置y轴范围
    chart->setTitle(QString::number(last_value));
#else
    // double vv = v_max < 10 && v_min < 10 ? 0.2 : 1;
    double vv = (v_max - v_min) / 5;
    mPlot->yAxis->setRange(v_min - vv, v_max + vv );
    mPlot->graph(0)->setData(x, values, true);
    mPlot->graph(1)->setData(x, limits, true);
    title->setText(QString::number(last_value, 'g', 4));
    mPlot->replot();
    // qDebug() << values.last() << last_value;
    // qDebug() << v_min << v_max << vv << values;

#endif
}

#ifdef QCHARTS
void FromChartView::createChartView() {
    //![1]
    chart = new QChart();
    // chart->setTheme(QChart::ChartThemeBlueCerulean);
    chart->setContentsMargins(0, 0, 0, 0);
    chart->setMargins(QMargins(0, 0, 0, 0));
    chart->setTitle("实时值");
    chart->legend()->setVisible(true);
    chart->legend()->setAlignment(Qt::AlignBottom);
    QFont f;
    f.setBold(true);
    f.setPointSize(24);
    f.setFamily("黑体");
    chart->setTitleFont(f);
    //chart->setOpacity(0.6); // 透明度0.0-1.0

    seriesValue = new QLineSeries();
    seriesAlarm = new QLineSeries();
    seriesAlarm->setName("限位");
    seriesValue->setName("项目名");
    seriesValue->setUseOpenGL(true);
    seriesAlarm->setUseOpenGL(true);

    QPen pen;
    // pen.setStyle(Qt::DashLine);
    // pen.setWidth(1);
    pen.setColor(Qt::red);
    seriesAlarm->setPen(pen);

    // pen.setStyle(Qt::DashLine);
    pen.setColor(Qt::yellow);
    // pen.setWidth(2);
    seriesValue->setPen(pen);

    /*设置横纵坐标的刻度值*/
    axisx =  new  QValueAxis();
    axisy =  new  QValueAxis();

    // axisx->setRange(-5, 155);  //设置x轴范围
    axisx->setGridLineVisible(false);
    axisx->setLabelsVisible(false);
    axisx->setLineVisible(false);

    // axisy->setRange(0, 100); //设置y轴范围
    axisy->setGridLineVisible(false);
    axisy->setLabelsVisible(false);
    axisy->setLineVisible(false);

    chart->addAxis(axisx, Qt::AlignBottom); //把刻度值传入进视图m_chart中显示
    chart->addAxis(axisy, Qt::AlignLeft);

    chart->addSeries(seriesValue);
    chart->addSeries(seriesAlarm);
    seriesValue->attachAxis(axisx);
    seriesValue->attachAxis(axisy);
    seriesAlarm->attachAxis(axisx);
    seriesAlarm->attachAxis(axisy);
    // axisx->setTickCount(5);

    ui->chartview->setChart(chart);
    ui->chartview->setRenderHint(QPainter::Antialiasing);
    // ui->chartview->hide();
    //![4]

    setAlarmFlag(false);
    setPointSizeX(pointSizeX);
}

#else

void FromChartView::createQCustomPlot() {
    mPlot = new QCustomPlot(this);
    mPlot->lower();// 放在底层
    mPlot->setGeometry(2, 2, this->width() - 4, this->height() - 4);
    mPlot->xAxis->setVisible(true);
    mPlot->xAxis->setTickLabels(false);

    QFont f;
    f.setBold(true);
    f.setPointSize(10);
    f.setFamily("微软雅黑");
    mPlot->xAxis->setLabelFont(f);
    mPlot->yAxis->setVisible(true);
    mPlot->yAxis->setTickLabels(true);
    mPlot->yAxis->setSubTicks(false);
    // mPlot->axisRect()->setupFullAxesBox();

    mPlot->plotLayout()->insertRow(0);
    title = new QCPTextElement(mPlot, "实时值", QFont("微软雅黑", 18, QFont::Bold));
    mPlot->plotLayout()->addElement(0, 0, title);

    mPlot->addGraph();
    mPlot->graph(0)->setPen(QPen(Qt::blue));
    mPlot->graph(0)->rescaleAxes();

    mPlot->addGraph();
    mPlot->graph(1)->setPen(QPen(Qt::red));
    mPlot->graph(1)->rescaleAxes(true);

    ui->label_alarm_ico->hide();
}
#endif

// void FromChartView::paintEvent(QPaintEvent *event) {
//     Q_UNUSED(event);
//     QPainter painter(this);
//     painter.setPen(Qt::blue);
//     painter.drawPolyline(polygon_v);

//     painter.setPen(Qt::red);
//     painter.drawPolyline(polygon_a);
//     // qDebug() << polygon_v;
// }
